package villa.tools.mq.views;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.wscep.widgets.UndoableStyledText;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import villa.tools.mq.preferences.MensajesSave;
import villa.tools.mq.preferences.Propiedades;

import com.ibm.mq.MQMessage;


class TabControlsO extends Composite{
	
	
	private SashForm splitPane = null;

	private Composite compWsdl = null;
	private Composite compRequest = null;
	private Composite compResponse = null;

	private Label otherLabel = null;
	private Label requestLabel = null;
	private Label responseLabel = null;

	UndoableStyledText styledTextOTHER = null;
	UndoableStyledText styledTextPut = null;
	UndoableStyledText styledTextGet = null;
	
	MQMessage msgPut = null;
	Mensaje msgGet = null;	
	
	private Label qManagerLabel = null;
	private Label queuePUTLabel = null;
	private Label queueGETLabel = null;
	Text qManagerText = null;
	Combo queuePUTCombo = null;
	Button queuePUTSave = null;
	Button queuePUTLoad = null;
	Text queuePUTText = null;
	Text queueGETText = null;
	private TabItem tabItem  = null;
	
	private Pattern xmlTagPattern = Pattern.compile("(<[^(><?)]+>)");
	private Pattern xmlAttributePattern = Pattern.compile("(\\S+=\".+\")");
	private Pattern xmlCommentPattern = Pattern.compile("(<!--?[^(?)]+?-->)");

	
	String getRequest() {
		return styledTextPut.getText();
	}

	int getRequestLength() {
		return styledTextPut.getText().length();
	}

	void setResponse(String response) {
		styledTextGet.setText(response);
	}
	
	public void setTabItem(TabItem ti){
		this.tabItem = ti;
	}
	
	/**
	 * @param parent
	 * @param style
	 */
	
	public TabControlsO(Composite parent, int style) {
		super(parent, style);
		
		try {
			
			
			FormLayout layout = new FormLayout();
			layout.marginWidth = 5;
			layout.marginHeight = 5;
			this.setLayout(layout);
			
			msgGet = new Mensaje();
			msgPut = new MQMessage();
			
			FormData formData = null;

			qManagerLabel = new Label(this, SWT.NONE);
			qManagerLabel.setText("Mensaje");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(0, 0);
			formData.width = 60;
			qManagerLabel.setLayoutData(formData);
			
			
			queuePUTCombo = new Combo(this, SWT.BORDER);
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(qManagerLabel, 5);
			formData.width = 200;
			queuePUTCombo.setLayoutData(formData);
			
			
			queuePUTLoad = new Button(this, SWT.CENTER);
			queuePUTLoad.setText("Load");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(queuePUTCombo, 5);
			formData.width = 40;
			queuePUTLoad.setLayoutData(formData);
			
			queuePUTSave = new Button(this, SWT.CENTER);
			queuePUTSave.setText("Save");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(queuePUTLoad, 5);
			formData.width = 40;
			queuePUTSave.setLayoutData(formData);
			
			
			
			
			

			// SPLIT PANE containing    wsdl | request | response
			splitPane = new SashForm(this, SWT.HORIZONTAL);
			formData = new FormData();
			formData.top = new FormAttachment(queuePUTCombo, 5);
			formData.left = new FormAttachment(0, 0);
			formData.bottom = new FormAttachment(100, 0);
			formData.right = new FormAttachment(100, 0);
			splitPane.setLayoutData(formData);

			// Composite containing    otherLabel 
			//                         wsdlText
			compWsdl = new Composite(splitPane, SWT.NONE);
			layout = new FormLayout();
			layout.marginWidth = 0;
			layout.marginHeight = 0;
			compWsdl.setLayout(layout);

			// Composite containing    requestLabel 
			//                         requestText
			compRequest = new Composite(splitPane, SWT.NONE);
			layout = new FormLayout();
			layout.marginWidth = 0;
			layout.marginHeight = 0;
			compRequest.setLayout(layout);

			// Composite containing    responseLabel 
			//                         responseText
			compResponse = new Composite(splitPane, SWT.NONE);
			layout = new FormLayout();
			layout.marginWidth = 0;
			layout.marginHeight = 0;
			compResponse.setLayout(layout);

			// otherLabel
			otherLabel = new Label(compWsdl, SWT.NONE);
			otherLabel.setText("QM URL:");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(0, 0);
			formData.width = 50;
			otherLabel.setLayoutData(formData);
			
			//QM URL TEXT
			qManagerText = new Text(compWsdl, SWT.BORDER);
			qManagerText.setEditable(true);
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(otherLabel, 5);
			formData.right = new FormAttachment(100, -5);
			qManagerText.setLayoutData(formData);

			
			// requestlabel
			requestLabel = new Label(compRequest, SWT.NONE);
			requestLabel.setText("PUT:");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(0, 0);
			formData.width = 30;
			//formData.right = new FormAttachment(100, 0);
			requestLabel.setLayoutData(formData);
			
			//PUT Text
			queuePUTText = new Text(compRequest, SWT.BORDER);
			//queuePUTText.setVisible(false);
			queuePUTText.setEditable(true);
			
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(requestLabel, 5);
			formData.right = new FormAttachment(100, -5);
			queuePUTText.setLayoutData(formData);
			
			
			// responselabel
			responseLabel = new Label(compResponse, SWT.NONE);
			responseLabel.setText("GET:");
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(0, 0);
			formData.width = 30;
			responseLabel.setLayoutData(formData);
			
			//GET TEXT
			queueGETText = new Text(compResponse, SWT.BORDER);
			queueGETText.setEditable(true);
			formData = new FormData();
			formData.top = new FormAttachment(0, 0);
			formData.left = new FormAttachment(responseLabel, 5);
			formData.right = new FormAttachment(100, -5);
			queueGETText.setLayoutData(formData);
			
			// WSDLTEXT
			styledTextOTHER = new UndoableStyledText(compWsdl, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, 1000);
			styledTextOTHER.setEditable(true);
			styledTextOTHER.setEnabled(true);
			styledTextOTHER.setText("Trace ...");
			styledTextOTHER.setWordWrap(Basic.getInstancia().getWrap());
			formData = new FormData();
			formData.top = new FormAttachment(otherLabel, 10);
			formData.left = new FormAttachment(0, 0);
			formData.bottom = new FormAttachment(100, 0);
			formData.right = new FormAttachment(100, 0);
			styledTextOTHER.setLayoutData(formData);

			
			// REQUESTTEXT
			styledTextPut = new UndoableStyledText(compRequest, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, 1000);
			styledTextPut.setEditable(true);
			styledTextPut.setEnabled(true);
			styledTextPut.setWordWrap(Basic.getInstancia().getWrap());
			formData = new FormData();
			formData.top = new FormAttachment(requestLabel, 10);
			formData.left = new FormAttachment(0, 0);
			formData.bottom = new FormAttachment(100, 0);
			formData.right = new FormAttachment(100, 0);
			styledTextPut.setLayoutData(formData);
			/*
			ModifyListener modifyListener = new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					colorXml(styledTextPut);
				}
			};
			styledTextPut.addModifyListener(modifyListener);
			*/
			
			// RESPONSETEXT
			styledTextGet = new UndoableStyledText(compResponse, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, 1000);
			styledTextGet.setEditable(true);
			styledTextGet.setEnabled(true);
			styledTextGet.setWordWrap(Basic.getInstancia().getWrap());
			formData = new FormData();
			formData.top = new FormAttachment(responseLabel, 10);
			formData.left = new FormAttachment(0, 0);
			formData.bottom = new FormAttachment(100, 0);
			formData.right = new FormAttachment(100, 0);
			styledTextGet.setLayoutData(formData);
			/*modifyListener = new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					colorXml(styledTextGet);
				}
			};
			styledTextGet.addModifyListener(modifyListener);
			*/
			
			
			queuePUTText.addKeyListener(new KeyListener() {
				public void keyPressed(KeyEvent e) {
					return;
				}
				public void keyReleased(KeyEvent e) {
					if (tabItem != null) {
						tabItem.setToolTipText(queuePUTText.getText());
						int len = queuePUTText.getText().length();
						if (len > 20) {
							tabItem.setText(queuePUTText.getText(0, 8) + "..." + queuePUTText.getText(len - 9, len - 1));
						} else {
							tabItem.setText(queuePUTText.getText());
						}
					}
				}
			});
			
			
			queuePUTLoad.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					if (tabItem != null) {
						
						String valor = MensajesSave.getInstancia().getPropiedad(queuePUTCombo.getText()+"_COLA");
						
						queuePUTText.setText(valor);
						tabItem.setToolTipText(queuePUTText.getText());
						
						int len = queuePUTCombo.getText().length();
						if (len > 20) {
							tabItem.setText(queuePUTText.getText(0, 8) + "..." + queuePUTText.getText(len - 9, len - 1));
						} else {
							tabItem.setText(queuePUTText.getText());
						}
						
						valor = MensajesSave.getInstancia().getPropiedad(queuePUTCombo.getText());
						styledTextPut.setText(valor);
						
						
					}
				}
			});
			
			
			queuePUTSave.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					
					Actions.saveMensajesF(queuePUTCombo.getText().toUpperCase(), styledTextPut.getText());
					Actions.saveMensajesF(queuePUTCombo.getText().toUpperCase()+ "_COLA", queuePUTText.getText());
					Actions.loadListF(queuePUTCombo);
				}
			});

			// Request 10% wieder than wsdl and response
			splitPane.setWeights(new int[] { 30, 40, 30 });

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void colorXml(StyledText styledText) {
		StyleRange styleRange = new StyleRange(0, styledText.getText().length(), Basic.getInstancia().getXmlTextColor(), null);
		styledText.setStyleRange(styleRange);

		Matcher matcher = xmlTagPattern.matcher(styledText.getText());
		while (matcher.find()) {
			int i = matcher.start();
			int j = matcher.end() - i;
			styleRange = new StyleRange(i, j, Basic.getInstancia().getXmlTagColor(), null);
			styledText.setStyleRange(styleRange);
		}

		matcher = xmlAttributePattern.matcher(styledText.getText());
		while (matcher.find()) {
			int i = matcher.start();
			int j = matcher.end() - i;
			styleRange = new StyleRange(i, j, Basic.getInstancia().getXmlAttributeColor(), null);
			styledText.setStyleRange(styleRange);
		}

		matcher = xmlCommentPattern.matcher(styledText.getText());
		while (matcher.find()) {
			int i = matcher.start();
			int j = matcher.end() - i;
			styleRange = new StyleRange(i, j, Basic.getInstancia().getXmlCommentColor(), null);
			styledText.setStyleRange(styleRange);
		}
	}
	
}

